﻿var FlavorTypeId = sessionStorage.getItem("id");
var FlavorTypeName = sessionStorage.getItem("name");
let MyFlavers = document.querySelector("#MyFlavors");
if (FlavorTypeId == null) {
    location.href("/Goods/FlavorTypeHtml");
}
var Name = null;
let where = {
    name: Name,
    flavorTypeName: FlavorTypeName,
    Ftid: FlavorTypeId
}
var loadTable
function BindFlavors(obj) {
    layui.use("table", function () {
        var table = layui.table;
        table.render({
            elem: "#test"
            , data: obj
            , align: 'center'
            , cellMinWidth: 80
            , cols: [[
                { field: 'name', title: '口味名称', align: 'center' }
                , { field: 'FtName', title: '口味类型', align: 'center' }
                , { title: '操作', align: 'center', toolbar: "#barDemo" }

            ]],
        });
        Peng.closeload(loadTable);
        //修改
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
           
            switch (layEvent) {
                case "edit":
                    let type;
                    $.get("/Goods/GetFlavorType", {}).then(res => {
                        Peng_Select.CreateElement("FlavormyType", res.data,'name', (res) => {
                            type = res.id
                        }, 'id', FlavorTypeId)
                    });
                    layer.open({
                        title: "编辑口味"
                        , area: ['600px', '400px']
                        , content: `<div class='Input-Control'> <div class="input-text"><p>口味名称</p></div> <input type="text" id="FlavorType" class="text-input" required name="name" autocomplete="off" value=${data.name} placeholder="请输入口味名称" /></div><div class='Input-Control'> <div class="input-text"><p>口味类型</p></div><div id="FlavormyType" class="Select-Control" style="width:80%"></div></div>`
                        , yes: function (index, text) {
                            $.post("/Goods/UpdateFlavor", {
                                id: data.id,
                                Ftid: type,
                                name: $("#FlavorType").val(),
                            }).then(res => {
                                if (res.code == 0) {
                                    Peng.Show(res.message, { type: 'Success' });
                                    layer.close(index);
                                    GetFlavor();
                                } else {
                                    Peng.Show(res.message, { type: 'Danger' });
                                }
                            });
                        }
                    });

            }
        });
    });
}


window.onload = function () {
    GetFlavor();
}
function GetFlavor() {
    loadTable = Peng.load({ box: 'Cols', style: 'circle', color: 'ZhutiSe', content: '加载口味数据中.....' });
    $.get("/Goods/GetFlavor", { keys: where.name, ftid: where.Ftid }).then(res => {
         BindFlavors(res.data);
    });
}
//模糊查询
var query = function () {
    where.name = $("#MyFlavors").val();
    where.Ftid = FlavorTypeId;
    GetFlavor();
    
}
//添加
let AddFlavors = document.querySelector("#AddFlavors");
AddFlavors.onclick = function () {
    let types;
    $.get("/Goods/GetFlavorType", {}).then(res => {
        Peng_Select.CreateElement("FlavormyTypes", res.data, 'name', (res) => {
            types = res.id
        }, 'id', FlavorTypeId)
    });
    layer.open({
        title: '添加口味',
        area: ['600px', '400px'],
        content: `<div class='Input-Control'> <div class="input-text"><p>口味名称</p></div> <input type="text" id="FlavorsType" class="text-input" required name="name" autocomplete="off" value="" placeholder="请输入口味名称" /></div><div class='Input-Control'> <div class="input-text"><p>口味类型</p></div><div id="FlavormyTypes" class="Select-Control" style="width:80%"></div></div>`,
        yes: function (index) {
            $.post("/Goods/AddFlavor", {
                Ftid: types,
                name: $("#FlavorsType").val(),
            }).then(res => {
              
                if (res.code == 0) {
                    
                    Peng.Show(res.message, { type: 'Success' });
                    layer.close(index);
                    GetFlavor();
                } else {
                    Peng.Show(res.message, { type: 'Danger' })
                }
            });
        }

    });
}


